package com.shkco.basic.controller;

import com.shkco.basic.util.AjaxResult;
import com.shkco.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping
    public AjaxResult upload(@RequestPart MultipartFile file){
        try {
            String filename = file.getOriginalFilename();
            String[] arr = filename.split("\\.");
            String name = FastDfsUtils.upload(file.getBytes(), arr[arr.length - 1]);
            return new AjaxResult().setResultObj(name);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"上传失败");
        }
    }

    @DeleteMapping
    public AjaxResult delete(@RequestParam("path") String path){
        try {
            String path1 = path.substring(1);
            String para1 = path1.substring(0,path1.indexOf("/"));
            String para2 = path1.substring(path1.indexOf("/")+1);
            FastDfsUtils.delete(para1,para2);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

}
